package com.dns.customers.aoptest.model;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * @author :  yulei
 * @data :  2018/6/11 8:57
 * @Version :  1.0
 **/

//管理Bean 生命周期的接口
public class Car  implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean {

    private String brand;
    private String color;
    private int maxSpeed;


    private BeanFactory beanFactory;
    private String beanName;

    //默认构造函数
    public Car(){
        System.out.println("调用Car()构造函数.");
    }
    //带参数构造函数

    public Car(String brand, String color, int maxSpeed) {
        this.brand = brand;
        this.color = color;
        this.maxSpeed = maxSpeed;
    }
    //未带参的方法
    public void introduce(){
        System.out.println("brand:"+brand+";color:"+color+";maxSpedd:"+maxSpeed);
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        System.out.println("调用setBrand属性.");
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }



    //BeanFactoryAware接口方法
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("调用BeanFactoryAware.setBeanFactory()方法");
        this.beanFactory=beanFactory;
    }

    //BeanNameAware
    @Override
    public void setBeanName(String name) {
        System.out.println("调用BeanNameAware.setBeanName()方法");
        this.beanName=beanName;
    }

    //DisposableBean
    @Override
    public void destroy() throws Exception {
        System.out.println("调用DisposableBean.destroy()方法");


    }
   //InitializingBean
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用InitializingBean.afterPropertiesSet()方法");
    }

    //通过<bean>的init-method属性指定的初始化方法
    public void myInit(){
        System.out.println("调用init-method所指定的myInit(),将maxSpeed设置为240.");
        this.maxSpeed=240;
    }

    //通过<bean>的destroy-method属性指定的销毁方法
    public void myDestroy(){
        System.out.println("调用destroy-method所指定的myDestroy()。");
    }



}
